import Duck from 'extensible-duck'
import produce from 'immer'

const initNewClass = {
  type: 'rectangle',
}

const initNewTag = {
  type: 'string',
}

export default new Duck({
  namespace: 'admin',
  store: 'projectConfig',
  types: ['NEW_CLASS_SET_TYPE', 'NEW_TAG_SET_TYPE'],
  initialState: {
    newClass: initNewClass,
    newTag: initNewTag,
  },
  reducer: produce((draft, action, duck) => {
    switch (action.type) {
      case duck.types.NEW_CLASS_SET_TYPE:
        draft.newClass.type = action.value
        break
      case duck.types.NEW_TAG_SET_TYPE:
        draft.newTag.type = action.value
        break
      default:
        break
    }
  }),
  selectors: {
    newClassType: state => state.projectConfig.newClass.type,
    newTagType: state => state.projectConfig.newTag.type,
  },
  creators: duck => ({
    actionSetNewClassType: value => ({ type: duck.types.NEW_CLASS_SET_TYPE, value }),
    actionSetNewTagType: value => ({ type: duck.types.NEW_TAG_SET_TYPE, value }),
  }),
})
